/*
 * @Author: xushuhao
 * @Email: xshmouse@163.com
 * @Date: 2020-06-11 11:01:39
 * @Last Modified by: xushuhao
 * @Last Modified time: 2020-07-30 08:23:26
 * @Description: 单图片/视频选择 支持拍照录像 调用原生相机
 */

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

typedef void GetPickImagePath(PickedFile path);
enum MediaType { image, video }

class SingleMediaPickerWidget extends StatefulWidget {
  final MediaType mediaType;
  GetPickImagePath callback;

  SingleMediaPickerWidget(
      {Key key, this.mediaType = MediaType.image, @required this.callback})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _SingleMediaPickerWidgettState();
  }
}

class _SingleMediaPickerWidgettState extends State<SingleMediaPickerWidget> {
  String shotTitle;
  String selectTitle;
  bool isVideo;
  final ImagePicker _picker = ImagePicker();

  @override
  Widget build(BuildContext context) {
    TextStyle _style =
        TextStyle(fontSize: 18, color: Color.fromRGBO(118, 118, 118, 1));
    return Column(
      children: <Widget>[
        GestureDetector(
          child: Container(
            color: Colors.transparent,
            alignment: Alignment.center,
            constraints: BoxConstraints.expand(height: 50),
            child: Text(
              shotTitle,
              style: _style,
            ),
          ),
          onTap: () {
            print('pz');
            widget.mediaType == MediaType.image
                ? _onImageButtonPressed(ImageSource.camera, context: context)
                : _onImageButtonPressed(ImageSource.camera);
            Navigator.pop(context);
          },
        ),
        Divider(
          color: Color.fromRGBO(151, 151, 151, 0.5),
          height: 2,
        ),
        GestureDetector(
          child: Container(
            color: Colors.transparent,
            alignment: Alignment.center,
            constraints: BoxConstraints.expand(height: 50),
            child: Text(
              selectTitle,
              style: _style,
            ),
          ),
          onTap: () {
            print('xc');
            widget.mediaType == MediaType.image
                ? _onImageButtonPressed(ImageSource.gallery, context: context)
                : _onImageButtonPressed(ImageSource.gallery);
            Navigator.pop(context);
          },
        ),
        Container(
          color: Color.fromRGBO(151, 151, 151, 0.3),
          constraints: BoxConstraints.expand(height: 6),
          child: Text(''),
        ),
        GestureDetector(
          child: Container(
            color: Colors.transparent,
            alignment: Alignment.center,
            constraints: BoxConstraints.expand(height: 50),
            child: Text(
              '取  消',
              style: _style,
            ),
          ),
          onTap: () {
            Navigator.pop(context);
          },
        ),
      ],
    );
  }

  _onImageButtonPressed(ImageSource source, {BuildContext context}) async {
    if (isVideo) {
      final PickedFile file = await _picker.getVideo(
          source: source, maxDuration: const Duration(seconds: 30));
      widget.callback(file);
    } else {
      try {
        final pickedFile = await _picker.getImage(
          source: source,
        );
        widget.callback(pickedFile);
      } catch (e) {
        widget.callback(null);
      }
    }
  }

  @override
  void initState() {
    shotTitle = widget.mediaType == MediaType.image ? '拍  照' : '录  像';
    selectTitle = widget.mediaType == MediaType.image ? '从相册选择' : '从相册选择';
    isVideo = widget.mediaType == MediaType.image ? false : true;
    super.initState();
  }
}
